document.addEventListener('DOMContentLoaded', (event) => {
    const membershipElement = document.getElementById('user-membership');

    membershipElement.addEventListener('click', () => {
        const membershipStatus = membershipElement.innerText;

        if (membershipStatus === '普通') {
            const upgrade = confirm('您当前是普通用户，是否升级为会员用户？');
            if (upgrade) {
                window.location.href = '/alipay/pay';
                updateMembershipStatus();
            }
        }
        // 初始加载时显示用户状态
        displayUserStatus();
    });
});

function updateMembershipStatus() {
    var user = JSON.parse(sessionStorage.getItem("user"));
    user.vip = true; // 假设支付成功后，将用户的vip属性设置为true
    sessionStorage.setItem("user", JSON.stringify(user));
    handleLogin(user);

    // 更新页面显示的会员状态
    $("#user-membership").text("会员");
}

function handleLogin(user) {
    // 这里处理登录后的逻辑，例如显示用户信息
    document.getElementById('user-profile').style.display = 'block';
    document.getElementById('user-name').innerText = user.name;
    $("#user-membership").text(user.vip ? "会员" : "普通");
}

function displayUserStatus() {
    var user = JSON.parse(sessionStorage.getItem("user"));
    if (user) {
        document.getElementById('user-profile').style.display = 'block';
        document.getElementById('user-name').innerText = user.name;
        $("#user-membership").text(user.vip ? "会员" : "普通");
    }
}